package plugins.schoolcard;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.datamodel.model.validator.ValidateResult;
import kd.bos.designer.table.AbstractValidator;
import kd.bos.entity.MainEntityType;
import kd.bos.entity.operate.OperateOptionConst;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.args.BeginOperationTransactionArgs;
import kd.bos.form.control.Control;
import kd.bos.form.control.Toolbar;
import kd.bos.form.control.events.ClickEvent;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.sdk.plugin.Plugin;

import java.util.Calendar;
import java.util.Date;
import java.util.EventObject;

/**
 * 单据界面插件
 */
public class CheckIn extends AbstractBillPlugIn implements Plugin {
    private static final String TARGET_BUTTON = "dcb4_checkin";
    private static final String TOOLBAR_KEY = "tbmain";

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Toolbar toolbar = this.getView().getControl(TOOLBAR_KEY);
        toolbar.addItemClickListener(this);
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        if (TARGET_BUTTON.equals(evt.getItemKey())) {

            // 创建操作选项并禁用消息提示
            OperateOption saveOption = OperateOption.create();
            saveOption.setVariableValue(OperateOptionConst.ISSHOWMESSAGE, "false");

            // 执行保存操作（禁用提示）
            this.getView().invokeOperation("save", saveOption);
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operation = e.getOperateKey();
        OperationResult result = e.getOperationResult();

        if (result != null && result.isSuccess()) {
            // 禁用所有操作的成功提示
            result.setShowMessage(false);

            // 操作链式触发
            if ("save".equals(operation)) {
                OperateOption submitOption = OperateOption.create();
                submitOption.setVariableValue(OperateOptionConst.ISSHOWMESSAGE, "false");
                this.getView().invokeOperation("submit", submitOption);
            }
            else if ("submit".equals(operation)) {
                OperateOption auditOption = OperateOption.create();
                auditOption.setVariableValue(OperateOptionConst.ISSHOWMESSAGE, "false");
                this.getView().invokeOperation("audit", auditOption);
            }
        }

        this.getView().showMessage("操作成功！");
    }

}
